package com.itboye.sunsun.product.center;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.homesensen.alert.ByAlert;
import com.itboye.sunsun.network.HttpRequest;
import com.itboye.sunsun.person.ui.HomeBean;
import com.itboye.sunsun.support.BaseActivity;
import com.itboye.sunsun.volley.MyJsonRequest;
import com.itboye.sunsun.volley.XErrorListener;
import com.itboye.sunsun.volley.XRequestListener;
import com.itboye.sunsunhome.R;

import java.util.ArrayList;

public class ProducenterGridviewActivity extends BaseActivity implements
		OnClickListener {
	BaseAdapter adapter;
	GridView gridviewProducenter;
	String title;
	TextView add_shap_title_tv;
	String parentid;
	ImageView close_icon;
	protected LinearLayout produceterLiner;

	@Override
	@SuppressLint("InlinedApi")
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.actiivty_producenter_gridview);
		showProgressDialog("加载中...", true);

		title = getIntent().getStringExtra("title");
		parentid = getIntent().getStringExtra("parentid");
		add_shap_title_tv.setText(title);
		close_icon.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		setStatusBarColor(R.color.home_blue);
		pullDown();

	}

	private void pullDown() {

		MyJsonRequest.Builder<ArrayList<HomeBean.HomeListBean>> builder = new MyJsonRequest.Builder<ArrayList<HomeBean.HomeListBean>>()
				.apiVer("100")
				.param("parentid", parentid)

				.typeKey("BY_ProductCenter_twocate")
				.requestListener(
						new XRequestListener<ArrayList<HomeBean.HomeListBean>>() {

							@Override
							public void onResponse(
									final ArrayList<HomeBean.HomeListBean> orderBean) {
								if (orderBean.size() > 0) {
									Log.d("namenaeanerwe",orderBean.get(0).getNotes()+"");
									produceterLiner.setVisibility(View.GONE);
									gridviewProducenter
											.setVisibility(View.VISIBLE);
									adapter = new ProductCenterAdapter(

									ProducenterGridviewActivity.this, orderBean);
									gridviewProducenter.setAdapter(adapter);
									gridviewProducenter
											.setOnItemClickListener(new OnItemClickListener() {

												@Override
												public void onItemClick(
														AdapterView<?> parent,
														View view,
														int position, long id) {
													// TODO Auto-generated
													// method
													// stub
													Intent intent = new Intent(
															ProducenterGridviewActivity.this,
															ProductCenterMyKefuActivity.class);
													intent.putExtra("title",orderBean.get(position).getName());
													intent.putExtra("id",orderBean.get(position).getId());
													intent.putExtra("notes",orderBean.get(position).getNotes());
													startActivity(intent);
												}
											});
								} else {

									gridviewProducenter
											.setVisibility(View.GONE);
									produceterLiner.setVisibility(View.VISIBLE);
								}
								try {
									closeProgressDialog();
								} catch (Exception e) {
									// TODO: handle exception
								}

							}

						}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code,
							String msg) {

						gridviewProducenter
								.setVisibility(View.GONE);
						produceterLiner.setVisibility(View.VISIBLE);
						System.out.println(msg+">>>>product---------");
						ByAlert.alert(msg);
						try {
							closeProgressDialog();
						} catch (Exception e) {
							// TODO: handle exception
						}
					}
				});

		MyJsonRequest<ArrayList<HomeBean.HomeListBean>> request = builder
				.build();
		HttpRequest.getDefaultRequestQueue().add(request);

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.close_icon:
			this.finish();
			break;

		default:
			break;
		}
	}

}
